(function() {
	'use strict';
	
	var Step = function(options) {
		var _default = {
		    id: null,
		    stepIndex: null,
		    unitId: null,
		    stepType: null,
		    stepInst: null,
		    stepLabel: null,
		    preStepIdx: null,
		    nextStepIdx: null,
		    stepCfgVal: null,
            stepCfgValMap: {}
        }
		return _.extend(_default, options);
	}
	
	var store = new Vuex.Store({
	    state: {
	    	unit: {
	    		id: guid(),
	    		unitCode: null,
	    		label: null,
	    		template: null,
	    		createUser: null,
	    		unitType: '1',
	    		batchType: 'daily',
	    		batchNo: moment().subtract('days', 0).format('YYYYMMDD'),
	    		batchOffset: 0,
	    		batchStep: 1, 
	    		descr: null,
	    		graph: null,
                createDate: '',
                lastupDate: null,
                unitExtCfg: null,
				state: '0',
                unitSteps: []
            },
            oper: dataId != null && dataId != '' ? 'edit' : 'add',
			originId: ''
	    },
	    mutations: {
	    	editUnit: function (state, options) {
	    		state.unit = _.extend(state.unit, options);
                state.originCode = state.unit.unitCode;
	    	},
	    	renderGraphXml: function (state, xml) {
	    		if (editorUi) {
	    			editorUi.refresh();
    				var doc = mxUtils.parseXml(xml);
	                var codec = new mxCodec(doc);
	                _.map(state.unit.unitSteps, function(step) {
	                	if (doc.getElementById(step.id)) {
		                	var modelData = '{}';
	                		try {
	                			var testData = JSON.parse(step.stepCfgVal);
	                			modelData = step.stepCfgVal;
	            			} catch(error) {
	            			}
	            			codec.setAttribute(doc.getElementById(step.id), 'modelData', modelData);
		                	codec.setAttribute(doc.getElementById(step.id).children[0], 'modelData', modelData);
	                	}
	                	return step;
	                })
	                codec.decode(doc.documentElement, editorUi.editor.graph.getModel());
	    		}
	    	},
	    	getUnitGraphXml: function (state) {
	    		var xml = editorUi.editor.getGraphXml().outerHTML;
	    		var doc = mxUtils.parseXml(xml);
                var codec = new mxCodec(doc);
                _.map(state.unit.unitSteps, function(step) {
                	if (doc.getElementById(step.id)) {
                		codec.setAttribute(doc.getElementById(step.id), 'modelData', '');
	                	codec.setAttribute(doc.getElementById(step.id).children[0], 'modelData', '');
                	}
                	return step;
                })
	    		state.unit.graph = doc.documentElement.outerHTML;
	    	},
	    	getStepData: function (state) {
	    		var cells = editorUi.editor.graph.getModel().cells,
	    		    stepCells = [],
	    		    edgeCells = [],
	    		    stepObj = {},
	    		    stepData = [],
	    		    indexObj = {},
	    		    index = -1;
	    		
	    		for(var i in cells){
	    			if (cells[i].isEdge()) {
	    				edgeCells.push(cells[i]);
	    			} else if (cells[i].type) {
	    				stepCells.push(cells[i]);
	    				var type = typeof cells[i].modelData;
	    				var modelData;
	    				try {
                			modelData = JSON.parse(cells[i].modelData);
            			} catch(error) {
            				modelData = type == 'object' && cells[i].modelData != null ? cells[i].modelData : {};
            			}
	    				var stepLabel = modelData.stepLabel;
                        delete modelData.stepLabel;
	    				var option = {
	    					id:  cells[i].id,
	    					stepIndex: cells[i].id,
	    					unitId: state.unit.id,
							unitCode:state.unit.unitCode,
	    					stepType: stepDefine[cells[i].name].stepType,
	    					itemType: cells[i].itemType,
	    					parentStepIdx: cells[i].parent ? (cells[i].parent.type ? cells[i].parent.id: null) : null,
	    					stepLabel: stepLabel || cells[i].getAttribute('label') || cells[i].value,
	    	    			stepInst: cells[i].name,
	    					stepCfgValMap: modelData ? modelData : {},
	    				}
	    				stepObj[cells[i].id] = new Step(option);
	    			}
	    		}
	    		
	    		for (var i in edgeCells) {
	    			var target =edgeCells[i].target;
	    			var source = edgeCells[i].source;
	    			if (target && source) {
	    				stepObj[target.id].preStepIdx = stepObj[target.id].preStepIdx ? stepObj[target.id].preStepIdx + ',' + source.id : source.id;
		    			stepObj[source.id].nextStepIdx = stepObj[source.id].nextStepIdx ? stepObj[source.id].nextStepIdx + ',' + target.id : target.id;
	    			}
	    		}
	    		
	    		var fun = function(step, parentStepIdx) {
	    			if (typeof step.stepIndex != 'number'&& step.parentStepIdx == parentStepIdx) {
	    				index++;
		    			step.stepIndex = index;
		    			indexObj[step.id] = step.stepIndex;
	    			} else {
	    				return false;
	    			}
	    			if (step.itemType == 'container') {
	    				setContainerStepIndex(step.id, stepObj);
	    			}
	    			if (step.nextStepIdx) {
	    				var idArr = step.nextStepIdx.split(',');
	    				for (var i in idArr) {
	    					fun(stepObj[idArr[i]], stepObj[idArr[i]].parentStepIdx);
	    				}
	    			}
	    		}
	    		
	    		var setContainerStepIndex = function(parentStepIdx, steps) {
	    			var containerSteps = _.filter(steps, function(step) {
		    			return step.parentStepIdx == parentStepIdx;
		    		});
		    		
		    		var startSteps = _.filter(containerSteps, function(step) {
		    			return step.preStepIdx == null;
		    		});
		    		
		    		if (startSteps.length == 0) {
		    			var isNotEndSteps = _.filter(steps, function(step) {
			    			return step.nextStepIdx != null;
			    		});
		    			if (isNotEndSteps.length == 0) {
		    				fun(steps[0], parentStepIdx);
		    			} else {
		    				fun(isNotEndSteps[0], parentStepIdx);
		    			}
		    		} else {
		    			for (var i in startSteps) {
		    				fun(startSteps[i], parentStepIdx);
		    			}
		    		}
	    		}
	    		
	    		setContainerStepIndex(null, stepObj);
	    		
	    		for(var i in stepObj){
	    			var preIdArr = stepObj[i].preStepIdx ? stepObj[i].preStepIdx.split(',') : [];
	    			var nextIdArr = stepObj[i].nextStepIdx ? stepObj[i].nextStepIdx.split(',') : [];
	    			var parentIdArr = stepObj[i].parentStepIdx ? stepObj[i].parentStepIdx.split(',') : [];
	    			preIdArr =_.map(preIdArr, function(id){
	    				return indexObj[id];
	    			});
	    			nextIdArr = _.map(nextIdArr, function(id){
	    				return indexObj[id];
	    			});
	    			parentIdArr = _.map(parentIdArr, function(id){
	    				return indexObj[id];
	    			});
	    			stepObj[i].preStepIdx = preIdArr.join(',') != '' ? preIdArr.join(',') : '-1';
	    			stepObj[i].nextStepIdx = nextIdArr.join(',') != '' ? nextIdArr.join(',') : '-1';
	    			stepObj[i].parentStepIdx = parentIdArr.join(',');
                    stepObj[i].stepCfgVal=JSON.stringify(stepObj[i].stepCfgValMap);
	    			stepData.push(stepObj[i]);
	    		}
	    		
	    		state.unit.unitSteps = stepData;
	    		this.commit('getUnitGraphXml');
	    	}
	    },
	    actions: {}
	});
	
	window.store = store;
}())